package com.jweb.framework.http.json;

import java.util.Map;

import org.springframework.context.MessageSourceResolvable;

import com.jweb.framework.core.Context;
import com.jweb.framework.core.channel.MessagesProvider;
import com.jweb.framework.core.channel.interceptors.ExceptionHandler;
import com.jweb.framework.core.ex.ValidationException;

/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：json报文解析异常处理类
 */
public class HttpJsonExceptionHandler implements ExceptionHandler {
	
	private String errorType = "msgType";
	private String errorCodeName = "rspCode";
	private String errorMsgName = "rspMsg";
	private MessagesProvider messagesProvider ;
	private String msgType = "E";
	
	private Map<String , String> messageCodeMapping ;
	
	private String defaultMappingCode ;
	
	public void setMessagesProvider(MessagesProvider messagesProvider){
		this.messagesProvider = messagesProvider ;
	}
	
	public void setMsgType(String msgType){
		this.msgType = msgType ;
	}
	
	public void setErrorType(String errorType){
		this.errorType = errorType ;
	}
	
	public void setErrorCodeName(String errorCodeName){
		this.errorCodeName = errorCodeName ;
	}
	
	public void setErrorMsgName(String errorMsgName){
		this.errorMsgName = errorMsgName ;
	}
	
	public void setMessageCodeMapping(Map<String , String> mapping){
		this.messageCodeMapping = mapping ;
	}
	
	public void setDefaultMappingCode(String code){
		this.defaultMappingCode = code ;
	}
	
	public void handleException(Throwable throwable , Context context) {
		
		if(throwable == null){
			return ;
		}
		String messageCode = throwable.getMessage() ;
		String defaultMsg = null ;
		Object message ;
		
		if(throwable instanceof ValidationException) {
			ValidationException exp = (ValidationException) throwable;
			message = exp.getMessage();
			context.setData(this.errorCodeName , "700200");
			context.setData(this.errorMsgName , message);
			return ;
		}
		
		if(throwable instanceof MessageSourceResolvable){
			MessageSourceResolvable exm = null ;
			if(throwable.getCause() instanceof MessageSourceResolvable){
				exm = (MessageSourceResolvable) throwable.getCause();
			}
			else {
				exm = (MessageSourceResolvable) throwable ;
			}
			
			if(exm != null){
				if(exm.getCodes() != null){
					messageCode = exm.getCodes()[0];
				}
				if(exm.getDefaultMessage() != null){
					defaultMsg = exm.getDefaultMessage();
				}
			}
		}
		
		if(messageCodeMapping != null){
			String mappedMessageCode = messageCodeMapping.get(messageCode);
			if(mappedMessageCode != null){
				messageCode = mappedMessageCode ;
			}
			else if(this.defaultMappingCode != null){
				messageCode = defaultMappingCode ;
			}
		}
		
		try {
			
			message = messagesProvider.resolveError(throwable , context.getLocale());
			
		} catch (Exception e) {
			if(defaultMsg != null){
				message = defaultMsg ;
			}
			else {
				message = messageCode ;
			}
		}
		
		if(this.errorType == null || this.errorType == ""){
			
		}
		else {
			context.setData(this.errorType, this.msgType);
		}
		if(this.errorCodeName==null || this.errorCodeName==""){
			
		}
		else {
			context.setData(this.errorCodeName , messageCode);
		}
		if(this.errorMsgName == null || this.errorMsgName == ""){
			
		}
		else {
			context.setData(this.errorMsgName , message);
		}
	}
}
